//index.js
//获取应用实例
import { getSellers, getAddressSite, getSellerType } from '../../utils/apis'
import { alert } from '../../utils/util'
const app = getApp()
Page({
  data: {
    imgUrls: [
      'https://bod-t.ezhanquan.com/cdn/pic/WechatIMG28.jpeg',
      'https://bod-t.ezhanquan.com/cdn/4c98296b7ebc47ec91fb4775be83a10e.jpeg',
      'https://bod-t.ezhanquan.com/cdn/41f0cd4b77aa4226b8e127bcece5fd29.jpeg'
    ],
    indicatorDots: true,
    indicatorColor: 'rgba(255,255,255,0.5)',
    indicatorActiveColor: '#FDD228',
    autoplay: true,
    interval: 5000,
    duration: 1000,
    selectedId: '1',
    selectArr: [{
        name: '综合排序',
        selectId: 1
      },
      {
        name: '销量最高',
        selectId: 2
      },
      {
        name: '评分最高',
        selectId: 3
      }
    ],
    category: [],
    hasMore: true,
    loading: false,
    siteData: null
  },
  onLoad: function() {
    this.initAddress()
  },
  itemclick(e) {
    let data = e.currentTarget.dataset.item
    let loginInfo = wx.getStorageSync('loginInfo')
    console.log(data);

    if (data.onlyToStudent === 1 && !loginInfo.learnNum) {
      alert("只针对学生开放")
      return
    }

    wx.navigateTo({
      url: `/pages/shop/show?id=${data.id}`
    })
  },
  initAddress() {
    var that = this
    this.invalidateData()
    getApp().getCurrentAddress(function(address) {
      getAddressSite({
          longitude: address.location.longitude,
          latitude: address.location.latitude
        })
        .then(result => {
          if (result) {
            that.setData({
              siteData: result
            })
            address['title'] = result.name
            that.setData({
              currentAddress: address
            })
            return Promise.all([that.loadData(), that.getSellerType()])
          }
        })
        .catch(err => {
          console.log(err)
        })
    })
  },
  selectHandle(e) {
    this.invalidateData()
    this.setData({
      selectedId: e.target.id
    })
    switch (e.target.id) {
      case '1':
        this.loadData()
        break
      case '2':
        this.loadData({ sale: 1 })
        break
      case '3':
        this.loadData({ sum: 1 })
        break
      default:
        this.loadData()
        break
    }
  },
  getSellerType() {
    let that = this
    getSellerType({
        siteId: this.data.siteData.id
      })
      .then(result => {
        that.setData({
          category: result
        })
        console.log(that.data.category)
      })
      .catch(err => {
        console.log(err)
      })
  },
  loadData(obj) {
    if (this.data.loading) {
      return
    }
    if (!obj) {
      obj = {}
    }
    var that = this
    var { page, size = 10 } = this.data
    this.setData({
      loading: true
    })
    getSellers({
        page,
        size,
        siteId: this.data.siteData.id,
        saleNumPerMonth: obj.sale,
        sumScore: obj.sum
      })
      .then(data => {
        var { shopList } = that.data
        wx.stopPullDownRefresh()
        that.setData({
          shopList: shopList ? shopList.concat(data.stores) : data.stores,
          page: page + 1,
          hasMore: data.count > (that.data.page + 1) * 10,
          loading: false
        })
      })
      .catch(err => {
        alert(err)
      })
  },
  invalidateData() {
    this.setData({
      page: 0,
      hasMore: true,
      loading: false,
      shopList: null
    })
  },
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      switch (this.data.selectedId) {
        case '1':
          this.loadData()
          break
        case '2':
          this.loadData({ sale: 1 })
          break
        case '3':
          this.loadData({ sum: 1 })
          break
        default:
          this.loadData()
          break
      }
    }
  },
  onPullDownRefresh() {
    this.invalidateData()
    switch (this.data.selectedId) {
      case '1':
        this.loadData()
        break
      case '2':
        this.loadData({ sale: 1 })
        break
      case '3':
        this.loadData({ sum: 1 })
        break
      default:
        this.loadData()
        break
    }
  },
  callback(address) {
    getApp().setCurrentAddress(address)
    this.initAddress()
  },
  onShareAppMessage() {
    return {
      title: '首页',
      path: '/pages/index/index'
    }
  }
})